package com.bw.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bw.user.Entity.UserEntity;
import com.bw.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;


/**
 * @ClassName UserController
 * @Author Hao_YunKai
 * @Date 2022/4/9 10:47
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /* 列表 */
    @GetMapping("/list")
    public String list(@RequestParam(value = "userName",defaultValue = "")String userName,
                       @RequestParam(value = "phone",defaultValue = "")String phone,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "3") Integer size,
                       Model model) {
        IPage<UserEntity> list = userService.list(userName,phone, page, size);
        model.addAttribute("list",list);
        return "user";
    }

    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request){

        return userService.fileUpload(file);
    }

    @RequestMapping(value = "downLoadExcel", method = RequestMethod.GET)
    public void downLoadExcel(HttpServletResponse response){
        userService.downLoadExcel(response);
    }
}
